package model3d
{
	import laya.d3.component.Script3D;
	import laya.d3.core.Camera;
	import laya.d3.core.Sprite3D;
	import laya.d3.math.Quaternion;
	import laya.d3.math.Vector3;
	import laya.display.Scene;
	import laya.events.Event;
	
	//	import shiyan.Show3D;
	/**
	 *默认绕着Z轴转 
	 * @author Administrator
	 * 
	 */	
	public class RotateScript extends Script3D
	{
		protected var lastMouseX:Number;
		protected var lastMouseY:Number;
		protected var yawPitchRoll:Vector3 = new Vector3();
		protected var resultRotation:Quaternion = new Quaternion();
		protected var tempRotationZ:Quaternion = new Quaternion();
		protected var tempRotationX:Quaternion = new Quaternion();
		protected var tempRotationY:Quaternion = new Quaternion();
		protected var isMouseDown:Boolean;
		protected var rotaionSpeed:Number = 0.01;
		protected var modle:Sprite3D;
		protected var scene:Scene;
		protected var rotationV:Quaternion;
		protected var positionV:Vector3;
		public function RotateScript()
		{
			super();
			
		}
		
		override public function onAwake():void
		{
			// TODO Auto Generated method stub
			super.onAwake();
			modle = owner as Sprite3D;
			Laya.stage.on(Event.MOUSE_DOWN, this, mouseDown);
			Laya.stage.on(Event.MOUSE_UP, this, mouseUp);
			Laya.stage.on(Event.MOUSE_OUT, this, mouseOut);
			rotationV=modle.transform.rotation.clone();
			positionV=modle.transform.position.clone();
		}
		
		
		public function reset():void{
			modle.transform.rotation=rotationV.clone();
			modle.transform.position=positionV.clone();
		}
		override public function onUpdate():void
		{
			// TODO Auto Generated method stub
			super.onUpdate();
			updateOwner();
		}
		
		public function mouseDown(e:Event):void{
			var touches:Array = e.touches;
			if(touches && touches.length >= 2)
			{
				isMouseDown=false;
				return;
			}
			modle.transform.localRotation.getYawPitchRoll(yawPitchRoll);
			lastMouseX = Laya.stage.mouseX;
			lastMouseY = Laya.stage.mouseY;
			isMouseDown = true;
		}
		public function mouseUp():void
		{
			// TODO Auto Generated method stub
			super.onMouseUp();
			isMouseDown = false;
			owner.event("mouseUp");
			//			trace("role.transform.localRotation:"+modle.transform.rotation.x,modle.transform.rotation.y,modle.transform.rotation.z,modle.transform.rotation.w);
			//			trace("role.transform.localPosition:"+modle.transform.localPosition.x,modle.transform.localPosition.y,modle.transform.localPosition.z);
		}
		public function mouseOut():void{
			isMouseDown = false;
		}
		protected function updateOwner():void {
			if (!isNaN(lastMouseX) && !isNaN(lastMouseY) && RockerView.angle==-1) {
				var scene:Scene =(owner as Camera).scene;
				if (isMouseDown) {
					var offsetX:Number = Laya.stage.mouseX - lastMouseX;
					var offsetY:Number = Laya.stage.mouseY - lastMouseY;
					if(Math.abs(offsetX)>Math.abs(offsetY)){
						yawPitchRoll.x += offsetX * rotaionSpeed * 0.1;
					}
					else{
						yawPitchRoll.y+=offsetY*rotaionSpeed*-0.1;
					}
					updateRotation();
				}
			}
			lastMouseX = Laya.stage.mouseX;
			lastMouseY = Laya.stage.mouseY;
		}
		
		protected function updateRotation():void {
			if (Math.abs(yawPitchRoll.y) < 1.50) {
				Quaternion.createFromYawPitchRoll(yawPitchRoll.x, yawPitchRoll.y, yawPitchRoll.z,tempRotationZ);
				tempRotationZ.cloneTo(modle.transform.localRotation);
				modle.transform.localRotation = modle.transform.localRotation;
				trace("mode:"+modle.transform.rotation.x,modle.transform.rotation.y,modle.transform.rotation.z,modle.transform.rotation.w);
			}
		}
		override public function onDisable():void{
			Laya.stage.off(Event.MOUSE_DOWN, this, mouseDown);
			Laya.stage.off(Event.MOUSE_UP, this, mouseUp);
			Laya.stage.off(Event.MOUSE_OUT, this, mouseOut);
		}
	}
}